<?php
include "../function.php";
include "../include/admin.class.php";
$pro=new Product();
$admin = new Admin();
$admin->limit(2);

if(isset($_POST['submit'])){
	if(!isset($_GET['good'])){//保存新商品
		//获得上一个商品编号
		$sql = "select p_id from speed_product order by p_id desc limit 1";
		$id = $pro->query($sql,'one');
		$p_id = $id['p_id'] + 1;
		$path0 = $pro->getImagePath($_POST['path'][0]);
		$path1 = $pro->getImagePath($_POST['path'][1]);
		$path2 = $pro->getImagePath($_POST['path'][2]);
		$path3 = $pro->getImagePath($_POST['path'][3]);
		$path4 = $pro->getImagePath($_POST['path'][4]);
		$path5 = $pro->getImagePath($_POST['path'][5]);
		//var_dump($path);
		//var_dump($main_image);exit();
		//var_dump($id);
		$data = array("p_id" => $p_id,
							 "name" => $_POST['name'],
							 "price" => $_POST['price'],
							 "remark" => $_POST['remark'],
							 "info" => $_POST['info'],
							 "color" => $_POST['color'],
							 "type" => $_POST['type'],
							 "path0" => $path0['path'],
							 "path1" => $path1['path'],
							 "path2" => $path2['path'],
							 "path3" => $path3['path'],
							 "path4" => $path4['path'],
							 "path5" => $path5['path'],
							 "model" => $_POST['model'],
							 "specification" => $_POST['specification'],
							 "create_time" => date("Y-m-d H:i:s",time()),
							 "create_user" => $_SESSION['login_admin']);
							
		//var_dump($data);exit();
		if($pro->addProduct($data))echo "<script type='text/javascript'>location.href='product_list.php';</script>";
		else echo "<script type='text/javascript'>alert('未知错误');history.go(-1);</script>";
	}else{//更新商品数据
		//echo "<script type='text/javascript'>alert('请将信息填写完整！');</script>";
		//foreach($_POST as $key => $val){
		//	$pram[$key] = $val;
		//}
		$temp = $pro->delAllImage($_GET['good']);
		$path0 = $pro->getImagePath($_POST['path'][0]);
		$path1 = $pro->getImagePath($_POST['path'][1]);
		$path2 = $pro->getImagePath($_POST['path'][2]);
		$path3 = $pro->getImagePath($_POST['path'][3]);
		$path4 = $pro->getImagePath($_POST['path'][4]);
		$path5 = $pro->getImagePath($_POST['path'][5]);
		$data = array("name" => $_POST['name'],
							 "price" => $_POST['price'],
							 "remark" => $_POST['remark'],
							 "info" => $_POST['info'],
							 "color" => $_POST['color'],
							 "type" => $_POST['type'],
							 "path0" => $path0['path'],
							 "path1" => $path1['path'],
							 "path2" => $path2['path'],
							 "path3" => $path3['path'],
							 "path4" => $path4['path'],
							 "path5" => $path5['path'],
							 "model" => $_POST['model'],
							 "specification" => $_POST['specification'],
							 "update_time" => date("Y-m-d H:i:s",time()),
							 "update_user" => $_SESSION['login_admin']);
							
		$temp = $pro->updateProduct($data,'p_id',$_GET['good']);
		//var_dump($temp);exit();
		echo "<script type='text/javascript'>location.href='product_list.php';</script>";
	}
}else{
	if(!isset($_GET['good'])){
		$pram['name']='';$pram['price']='';$pram['remark']='';$pram['info']='';
		$pram['color']='';$pram['model']='';$pram['specification']='';
		for($n = 0;$n<6;$n++){
			$img[$n] = '';
			$path[$n] = '';
		}
	}else{
		$pram = $pro->getAllByField('p_id',$_GET['good']);
		$images = $pro->getImage($_GET['good']);
		//var_dump($images);
		foreach($images as $key=>$val){		
			$img[] = $pro->getImageIdByPath($val);
			if($val == '')$path[] = '<div><img src="../images/no_img.png"></div>';
			else $path[] = '<div><img src="../'.$val.'"><input type="hidden" class="field" value="'.$key.'"><a class="del_image" href="#">删除</a></div>';
		}
		//var_export($tt);
	}
}

?>